﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace map.member
{
    public partial class pinfo : System.Web.UI.Page
    {
        private void InitMap()
        {
            string ScriptTag = "";


            ScriptTag =
                "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                + "" + "39.917043746896674,'" + "2222" + "');</script>";

            ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitMap();
            if (!IsPostBack)
            {
                Literal_map.Text = RuleCode.Util.getmapurl();
                dealrequest();
                string sheng = zfunction.Province;
                RuleCode.Table.县 x = RuleCode.Table.县.Find(" 省编码 = '" + sheng + "' ");
                DropDownList4.Items.Clear();
                foreach (DataRow item in x.MyTable.Rows)
                {
                    DropDownList4.Items.Add(item["县编码"].ToString());
                }
                bind11();
            }

        }

        private void dealrequest()
        {
            if (AliApi.appUserId == "")
            {
                Response.Redirect( ResolveUrl("~/member/login.aspx")+"?redirecturl=" + this.Request.Url);
            }
        }

        private void bind11()
        {
            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            TextBox1.Text = u.cbuserid.ToString();//邮件
            TextBox2.Text = u.cbusernickname.ToString();//昵称
            TextBox12.Text = u.cbname.ToString();//姓名
            TextBox13.Text = u.passportid.ToString(); //身份证

            TextBox6.Text = u.JuniorSchoolName.ToString();//初中所在学校
            DropDownList1.Text = u.JuniorSchoolGraduateYear.ToString();//初中毕业年份
            TextBox7.Text = u.SeniorSchoolName.ToString();//高中所在学校
            DropDownList2.Text = u.SeniorSchoolGraduateYear.ToString();//高中毕业年份
            TextBox8.Text = u.ColleageName.ToString();//大学
            DropDownList3.Text = u.ColleageGraduateYear.ToString();//大学毕业
            TextBox9.Text = u.LastEmployeeName.ToString();

            TextBox14.Text = u.cbqq.ToString();
            TextBox15.Text = u.cbmsn.ToString();
            TextBox16.Text = u.cbww.ToString();
            TextBox17.Text = u.email2.ToString();
            TextBox18.Text = u.jcmobile.ToString();


            Bind0();
            if (u.县编码.ToString() == "") return;
            DropDownList4.Text = u.县编码.ToString();
            Bind1();
            if (u.区编码.ToString() == "") return;
            DropDownList5.Text = u.区编码.ToString();
            Bind2();
            if (u.区域编码.ToString() == "") return;
            DropDownList6.Text = u.区域编码.ToString();
            Bind3();
            if (u.cbUserBizArea.ToString() == "") return;
            DropDownList7.Text = u.cbUserBizArea.ToString();
            Bind4();
            TextBox10.Text = u.taobaolong.ToString();
            TextBox11.Text = u.taobaolat.ToString();
            newDomain.Text = u.Domain1.StringValue;
        }



        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string xian = DropDownList4.Text;
            //RuleCode.Table.区 x = RuleCode.Table.区.Find(" 县编码 = '" + xian + "' ");
            //DropDownList5.Items.Clear();
            //foreach (DataRow item in x.MyTable.Rows)
            //{
            //    DropDownList5.Items.Add(item["区编码"].ToString());

            //}
            //InitMap();
            Bind1();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind2();
            //RuleCode.Table.区域 x = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList5.Text + "' "
            //   + " and 县编码 = '" + DropDownList4.Text + "' ");
            //DropDownList6.Items.Clear();
            //foreach (DataRow item in x.MyTable.Rows)
            //{
            //    DropDownList6.Items.Add(item["区编码"].ToString());

            //}
            //InitMap();
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind3();
        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind4();
        }
        /// <summary>
        /// 基本信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            cbuser u = cbuser.Find(" cbusernickname = '" + TextBox2.Text + "'");
            if (u.cbusernickname != TextBox2.Text)
            {
                if (u.Count > 0)
                {
                    MessageBox.Show(this.UpdatePanel2, "该昵称已经存在，操作取消.");
                    return;
                }
            }
            u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            //u.cbuserid.Value = TextBox1.Text;
            u.cbusernickname.Value = TextBox2.Text ;
            u.cbname.Value = TextBox12.Text;
            u.passportid.Value = TextBox13.Text;
            u.Modify();
            MessageBox.Show(this.UpdatePanel2, "操作成功.");
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            
            if (TextBox4.Text != TextBox5.Text)
            {
                MessageBox.Show(this.UpdatePanel2, "两次输入的新密码不同,操作不成功.");
                return;
            }
            if (TextBox4.Text != "")
            {
                MessageBox.Show(this.UpdatePanel2, "密码不能为空.");
                return;
            }
            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            if (TextBox3.Text == u.cbpassword.ToString())
            {
                u.cbpassword.Value = TextBox4.Text;
                u.Modify();
                MessageBox.Show(this.UpdatePanel2, "操作成功.");
            }
            else
            {
                MessageBox.Show(this.UpdatePanel2, "原密码不对,操作不成功.");
            }
        }
        /// <summary>
        /// 详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            u.JuniorSchoolName.Value = TextBox6.Text;
            u.JuniorSchoolGraduateYear.Value =zfunction.str2int( DropDownList1.Text);
            u.SeniorSchoolName.Value = TextBox7.Text;
            u.SeniorSchoolGraduateYear.Value = zfunction.str2int( DropDownList2.Text);
            u.ColleageName.Value = TextBox8.Text;
            u.ColleageGraduateYear.Value = zfunction.str2int(DropDownList3.Text);
            u.LastEmployeeName.Value = TextBox9.Text;
            u.cbqq.Value = TextBox14.Text;
            u.cbmsn.Value = TextBox15.Text;
            u.cbww.Value =TextBox16.Text;
            u.email2.Value = TextBox17.Text;
            u.jcmobile.Value = TextBox18.Text;
            u.cbaddress.Value = TextBox19.Text;
            u.Modify();
            MessageBox.Show(this.UpdatePanel2, "操作成功.");
        }
        /// <summary>
        /// 地理位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            u.省编码.Value = zfunction.Province;
            u.县编码.Value = DropDownList4.Text;
            u.区编码.Value = DropDownList5.Text;
            u.区域编码.Value =DropDownList6.Text;
            u.cbUserBizArea.Value = DropDownList7.Text;
            u.taobaolong.Value =zfunction.str2double( TextBox10.Text);
            u.taobaolat.Value = zfunction.str2double(TextBox11.Text);
            u.Modify();
            MessageBox.Show(this.UpdatePanel2, "操作成功.");
        }

        /// <summary>
        /// 绑定省
        /// </summary>
        private void Bind0()
        {
            string sheng = zfunction.Province;
            RuleCode.Table.县 x = RuleCode.Table.县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList4.Items.Clear();
            DropDownList5.Items.Clear();
            DropDownList6.Items.Clear();
            DropDownList7.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList4.Items.Add(item["县编码"].ToString());
            }

            if (x.Count > 0)
            {
                Bind1();
            }
        }
        /// <summary>
        /// 绑定县
        /// </summary>
        private void Bind1()
        {
            string xian = DropDownList4.Text;
            RuleCode.Table.区 q = RuleCode.Table.区.Find(" 县编码 = '" + xian + "' ");
            DropDownList5.Items.Clear();
            DropDownList6.Items.Clear();
            DropDownList7.Items.Clear();
            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList5.Items.Add(item["区编码"].ToString());
            }
            if (q.Count > 0)
            {
                Bind2();
            }
            else
            {
                //todo:用geocode显示该县
                string provice = zfunction.Province;
                string county = DropDownList4.SelectedValue;
                string query = provice + "," + county;
                string ScriptTag = "showAddress('" + query + "');";
                ScriptManager.RegisterStartupScript(
                this.UpdatePanel2, this.UpdatePanel2.GetType(), "bodyevent3", ScriptTag, true);

            }
        }
        /// <summary>
        /// 绑定区,镇,朝阳区
        /// </summary>
        private void Bind2()
        {
            RuleCode.Table.区域 q = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList5.Text + "' "
               + " and 县编码 = '" + DropDownList4.Text + "' ");
            DropDownList6.Items.Clear();
            DropDownList7.Items.Clear();
            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList6.Items.Add(item["区域编码"].ToString());
            }
            if (q.Count > 0)
            {
                Bind3();
            }
            else
            {
                RuleCode.Table.区 q1 = RuleCode.Table.区.Find(" 区编码 = '" + DropDownList5.Text + "' ");
                markpos2(q1.taobaolat.ToString(), q1.taobaolong.ToString(), q1.区编码.ToString());
            }
        }
        /// <summary>
        /// 小区域,呼家楼
        /// </summary>
        private void Bind3()
        {
            RuleCode.Table.BizArea q = RuleCode.Table.BizArea.Find(" 区编码 = '" + DropDownList5.Text + "' "
               + " and 县编码 = '" + DropDownList4.Text + "' "
               + " and 区域编码 = '" + DropDownList6.Text + "' "
               );
            DropDownList7.Items.Clear();
            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList7.Items.Add(item["BizAreaName"].ToString());
            }
            if (q.Count > 0)
            {
                Bind4();
            }
            else
            {
                RuleCode.Table.区域 q1 = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList5.Text + "' "
                    + " and 县编码 = '" + DropDownList4.Text + "' "
                    + " and 区域编码 = '" + DropDownList6.Text + "' "
                    );
                markpos2(q1.taobaolat.ToString(), q1.taobaolong.ToString(), q1.区编码.ToString());
            }

        }
        /// <summary>
        /// 大厦,商城,小区等,京广中心
        /// </summary>
        private void Bind4()
        {
            //显示所选地标的位置
            RuleCode.Table.BizArea x = RuleCode.Table.BizArea.Find(" 区编码 = '" + DropDownList5.Text + "' "
                                                                   + " and 县编码 = '" + DropDownList4.Text + "' " +
                                                                   " and 区域编码 = '" + DropDownList6.Text + "' " +
                                                                   " and BizAreaName = '" + DropDownList7.Text + "' ");
            markpos2(x.taobaolat.ToString(), x.taobaolong.ToString(), x.BizAreaName.ToString());
        }

        private void markpos2(string taobaolat, string taobaolong, string name)
        {
            string ScriptTag = "";
            if (!IsPostBack)
            {

                if (taobaolong == "0" || taobaolat == "0")
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + name + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + taobaolong + ","
                        + taobaolat + ",'" + name + "');</script>";
                }
                //Bug:修改了key为唯一值，解决绑定地址时只显示绑定每一级的第一个位置，显示用户的地址位置时，由于Key相同，不会注册显示代码。
                //fixed@20090901 by alpha
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                    taobaolong+taobaolat+name, ScriptTag, false);
                
            }
            else
            {
                //改变了值的情况、
                ScriptTag = "markpos2(" + taobaolong + ","
                          + taobaolat + ",'" + name + "');";

                ScriptManager.RegisterStartupScript(
                this.UpdatePanel4, this.UpdatePanel4.GetType(), "bodyevent367", ScriptTag, true);
            }

        }

        protected void changeDomain_Click(object sender, EventArgs e)
        {
            string domain = newDomain.Text;
            cbuser userd = cbuser.Find("Domain1 ='" + domain + "'");
            if (userd.Count > 0)
            {
                showDomainMess.Text = "域名已存在。";

            }
            else
            {
                cbuser u2 = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
                u2.Domain1 = domain;
                u2.Modify();
                showDomainMess.Text = "个性化域名设置成功。";
            }
        }

        protected void newDomain_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
